package com.example.security.config;

import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

@Configuration
public class PasswordEncoderConfig {

    private static PasswordEncoder pe;

    /**
     * 配置密码加密方式
     * @return
     */
    @Bean
    @ConditionalOnMissingBean
    public PasswordEncoder passwordEncoder(){
        pe = new BCryptPasswordEncoder();
        return pe;
    }

    public static String passwordEncryption(String password){
        return pe.encode(password);
    }

    /**
     * 密码匹配
     * @param rawPassword       前端传递的密码
     * @param encodedPassword   数据库密码
     * @return
     */
    public static boolean passwordMatches(CharSequence rawPassword, String encodedPassword){
        return pe.matches(rawPassword, encodedPassword);
    }
}
